package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.di.component.DaggerUserSetEditInfoSetTxtComponent;
import com.kmproject.haifu.mvp.contract.UserSetEditInfoSetTxtContract;
import com.kmproject.haifu.mvp.presenter.UserSetEditInfoSetTxtPresenter;
import com.xw.repo.XEditText;

import org.buffer.android.buffertextinputlayout.BufferTextInputLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/24/2019 13:58
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class UserSetEditInfoSetTxtActivity extends BaseActivity<UserSetEditInfoSetTxtPresenter> implements UserSetEditInfoSetTxtContract.View {

    @BindView(R.id.UserSetEditInfoSetTxt_cancle_txt)
    TextView UserSetEditInfoSetTxtCancleTxt;
    @BindView(R.id.UserSetEditInfoSetTxt_title_txt)
    TextView UserSetEditInfoSetTxtTitleTxt;
    @BindView(R.id.UserSetEditInfoSetTxt_sure_txt)
    TextView UserSetEditInfoSetTxtSureTxt;
    @BindView(R.id.user_content_txt)
    XEditText userContentTxt;
    @BindView(R.id.user_gxqm_txt)
    EditText userGxqmTxt;

//    @BindView(R.id.user_gxqm_BufferTextInputLayout)
//    BufferTextInputLayout userGxqmBufferTextInputLayout;
    @BindView(R.id.user_sex_lay)
    LinearLayout userSexLay;
    @BindView(R.id.setEditInfoSet_man_img)
    ImageView setEditInfoSetManImg;
    @BindView(R.id.setEditInfoSet_woman_img)
    ImageView setEditInfoSetWomanImg;
    @BindView(R.id.user_sex_man_lay)
    LinearLayout userSexManLay;
    @BindView(R.id.user_sex_woman_lay)
    LinearLayout userSexWomanLay;
    @BindView(R.id.user_gxqm_txt_FrameLayout)
    FrameLayout user_gxqm_txt_FrameLayout;
    @BindView(R.id.user_gxqm_txt_num)
    TextView user_gxqm_txt_num;

    private String sex="";
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserSetEditInfoSetTxtComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_set_edit_info_set_txt; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (getIntent().getStringExtra("type").equals("nc")) {
            user_gxqm_txt_FrameLayout.setVisibility(View.GONE);
            userSexLay.setVisibility(View.GONE);
            userContentTxt.setVisibility(View.VISIBLE);
            userContentTxt.setText(getIntent().getStringExtra("data"));
            UserSetEditInfoSetTxtTitleTxt.setText(ArmsUtils.getString(this,R.string.user_set_title_sznc));
        } else if (getIntent().getStringExtra("type").equals("gxqm")) {
            user_gxqm_txt_FrameLayout.setVisibility(View.VISIBLE);
            userSexLay.setVisibility(View.GONE);
            userContentTxt.setVisibility(View.GONE);
            userGxqmTxt.setText(getIntent().getStringExtra("data"));
            UserSetEditInfoSetTxtTitleTxt.setText(ArmsUtils.getString(this,R.string.user_set_title_szgxqm));

        } else {
            //性别选择
            user_gxqm_txt_FrameLayout.setVisibility(View.GONE);
            userSexLay.setVisibility(View.VISIBLE);
            userContentTxt.setVisibility(View.GONE);
            sex=getIntent().getStringExtra("data");
            UserSetEditInfoSetTxtTitleTxt.setText(ArmsUtils.getString(this,R.string.user_set_title_szxb));
            sex=getIntent().getStringExtra("data");
//            if ("男".equals(getIntent().getStringExtra("data")))
//            {
//                showImg(true);
//            }else {
//                showImg(false);
//            }
          showImg(sex);
        }
        setBtnStatus(false);

        userContentTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().equals(getIntent().getStringExtra("data")))
                {
                    setBtnStatus(false);
                }else {
                    setBtnStatus(true);
                }
            }
        });

        userGxqmTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().equals(getIntent().getStringExtra("data")))
                {
                    setBtnStatus(false);
                }else {
                    setBtnStatus(true);
                }
                     if (s.length()<31)
                     {
                         user_gxqm_txt_num.setText(s.length()+"/30");
                     }

            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.UserSetEditInfoSetTxt_cancle_txt, R.id.UserSetEditInfoSetTxt_sure_txt, R.id.user_sex_man_lay, R.id.user_sex_woman_lay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.UserSetEditInfoSetTxt_cancle_txt:
                killMyself();
                break;
            case R.id.UserSetEditInfoSetTxt_sure_txt:

                if (getIntent().getStringExtra("type").equals("nc")) {
                    mPresenter.updateUserNickName(userContentTxt.getText().toString().trim());
                } else if (getIntent().getStringExtra("type").equals("gxqm")) {
                    mPresenter.updateUserDesc(userGxqmTxt.getText().toString().trim());
                } else {
                    //性别选择
                    mPresenter.updUserSex(sex);
                }

                break;
            case R.id.user_sex_man_lay:
                sex="男";
                showImg(sex);
                if (sex.equals(getIntent().getStringExtra("data")))
                {
                    setBtnStatus(false);
                }else {
                    setBtnStatus(true);
                }
                break;
            case R.id.user_sex_woman_lay:
                sex="女";
                showImg(sex);
                if (sex.equals(getIntent().getStringExtra("data")))
                {
                    setBtnStatus(false);
                }else {
                    setBtnStatus(true);
                }
                break;
        }
    }

    /**
     * 控制性别按钮图标显示隐藏
     *
     * @param sex
     */
    public void showImg(String sex) {

        if ("男".equals(sex)) {
            setEditInfoSetManImg.setVisibility(View.VISIBLE);
            setEditInfoSetWomanImg.setVisibility(View.INVISIBLE);
        } else  if ("女".equals(sex)){
            setEditInfoSetManImg.setVisibility(View.INVISIBLE);
            setEditInfoSetWomanImg.setVisibility(View.VISIBLE);
        }else {
            setEditInfoSetManImg.setVisibility(View.INVISIBLE);
            setEditInfoSetWomanImg.setVisibility(View.INVISIBLE);
            setBtnStatus(false);
        }
    }

    /**
     * 设置登录按钮状态
     * @param isClick
     */
    public void setBtnStatus(boolean isClick)
    {
        if (isClick)
        {
            UserSetEditInfoSetTxtSureTxt.setEnabled(true);
            UserSetEditInfoSetTxtSureTxt.setBackgroundResource(R.drawable.button_yellow_bg);
            UserSetEditInfoSetTxtSureTxt.setTextColor(getResources().getColor(R.color.color_191919));
        }else {
            UserSetEditInfoSetTxtSureTxt.setEnabled(false);
            UserSetEditInfoSetTxtSureTxt.setBackgroundResource(R.drawable.button_gray_bg);
            UserSetEditInfoSetTxtSureTxt.setTextColor(getResources().getColor(R.color.color_EDEDED));
        }
    }

    @Override
    public void updateSuccess() {
        Intent intent=new Intent();
        intent.putExtra("type",getIntent().getStringExtra("type"));
        if (getIntent().getStringExtra("type").equals("nc")) {
            intent.putExtra("data",userContentTxt.getText().toString().trim());
        } else if (getIntent().getStringExtra("type").equals("gxqm")) {

            intent.putExtra("data",userGxqmTxt.getText().toString().trim());
        } else {
            //性别选择
            intent.putExtra("data",sex);
        }
        setResult(RESULT_OK,intent);
        killMyself();
    }
}
